﻿(function($){
    var w=this;
    $.fn.extend({
        workyear:function(options){
            var defaults={
                title:'参加工作年份',
				titleM:'月份',
                divClass:'work_show',
                popDiv:'popupDiv_workExpYear',
                yHiddenId:'',
                mHiddenId:'',
				left:0,
				languageId:1,
				close: null
            };
            options= $.extend({},defaults,options);
			if( options.languageId == 1 ){
				options.title = '参加工作年份';
				options.titleM = '月份';
			}else{
				options.title = 'Years of work';
				options.titleM = 'Month';
			};
            var inputR=$(this);
            var div=$(this).closest('.'+options.divClass);
            init.call(div);
            $(this).click(function(){
				if( $(this).siblings('#'+options.popDiv).css("visibility") == "hidden" ){
					showDiv('y');
				}else{
					hideResult();
				};
            });
            function setTable(y){
                var dhtml='';
                if(y==='y'){
                    var data=inputR.attr('datay');
                    var dataArr=data.split('@');
                    var dataLen=dataArr.length;
                    var dhArray=[];
                    if(dataLen>0){
                        for(var key in dataArr){
                            if(dataArr[key]){
                                var dR=dataArr[key].split('|');
								if(	options.languageId == 1 ){
									if( key > 2 ){
										dhArray.push('<td cls="y" width="20%" val="'+dR[0]+'">'+dR[1]+'年</td>');
									}else{
										if( key == 2 ){
											dhArray.push('<td cls="y" width="40%" colspan="2" val="'+dR[0]+'"><span class="fontWeit fr">'+dR[1]+'</span></td>');
										}else{
											dhArray.push('<td cls="y" width="60%" colspan="3" val="'+dR[0]+'"><span class="fontWeit fl">'+dR[1]+'</span></td>');
										};
										
									};
								}else{
									if( key > 2 ){
										dhArray.push('<td cls="y" width="20%" val="'+dR[0]+'">'+dR[1]+'</td>');
									}else{
										if( key == 2 ){
											dhArray.push('<td cls="y" width="40%" colspan="2" val="'+dR[0]+'"><span class="fontWeit fr">'+dR[1]+'</span></td>');
										}else{
											dhArray.push('<td cls="y" width="60%" colspan="3" val="'+dR[0]+'"><span class="fontWeit fl">'+dR[1]+'</span></td>');
										};
										
									};
								};
                            }
                        };
                        dhtml+='<tr class="experience_tr">';
                        for(var i= 0,l=dhArray.length;i<l;i++){
                            dhtml+=dhArray[i];
                            if(i===1||(i>0&&(i-1)%5==0)&&i<l-1){
                                dhtml+='</tr><tr><td class="line" colspan="5"><img height="1" width="1" src="http://my.zhaopin.com/images/blank.gif"></td></tr><tr>';
                            }
                        }
                        dhtml+='</tr>';
                    }
                }else if(y==='m'){
                    var mArray=[];
                    mArray.push('<tr>');
                    for(var i=1;i<=12;i++){
						if( options.languageId == 1 ){
							mArray.push('<td cls="m" width="20%" val="'+i+'">'+i+'月</td>');
						}else{
                        	mArray.push('<td cls="m" width="20%" val="'+i+'">'+i+'</td>');
						}
                        if(i<12&&i%5==0){
                            mArray.push('</tr><tr><td class="line" colspan="5"><img height="1" width="1" src="http://my.zhaopin.com/images/blank.gif"></td></tr><tr>');
                        }
                    }
                    mArray.push('</tr>');
                    dhtml=mArray.join('');
                }
                return dhtml;
            }
            function setBottom(div,y){
                if(y==='m'){
					if( options.languageId == 1 ){
                    	div.find('.bottom').html('<span style="display:none>[修改年份]</span>');
					}else{
						div.find('.bottom').html('<span style="display:none>[Modify years]</span>');
					};
                }else if(y==='y'){
                    div.find('.bottom').html('');
                }
            }
            function setButton(){
                var year=$('#'+options.yHiddenId).val();
                var month=$('#'+options.mHiddenId).val();
				if(options.languageId == 1 ){
                	inputR.val(year+'年'+month+'月');
				}else{
					inputR.val(year+'.'+month);
				};
            }
            function init(){
                var shtml='<div id="'+options.popDiv+'" class="zp_mr_workYear" style="visibility: hidden; left: '+options.left+'; top:'+div.height()+'px;">';
                shtml+='<div class="title">'+options.title+'</div>';
                shtml+='<div class="content">';
                shtml+='<table cellspacing="1" cellpadding="0" border="0"><tbody>';
                shtml+='</tbody></table>';
                shtml+='</div>';
                shtml+='<div class="bottom"></div>';
                shtml+='</div>';
                this.append(shtml);
                $('#'+options.popDiv).on('mouseover mouseout','td',function(event){
                    if(event.type=='mouseover'){
                        if(!$(this).parent().hasClass("experience_tr")){
                            $(this).addClass('focus');
                        }else{
                            $(this).find("span").css({background:"#84d4f9",color:"#fff"})
                        }
                        
                    }else if(event.type=='mouseout'){
                        if(!$(this).parent().hasClass("experience_tr")){
                            $(this).removeClass('focus');
                        }else{
                            $(this).find("span").css({background:"#fff",color:"#69a7fe"})
                        }
                    }
                });
                $('#'+options.popDiv).on('click','td[cls="y"]',function(){
                    var tdVal=$(this).attr('val');
                    $('#'+options.yHiddenId).val(tdVal);
                    if(tdVal==0||tdVal==2003){
                        inputR.val($(this).text());
                        $('#'+options.mHiddenId).val('1');
                        hideResult();
                        return;
                    }
                    $('#'+options.mHiddenId).val()||$('#'+options.mHiddenId).val('1');
                    setButton();
                    showDiv('m');

                });
                $('#'+options.popDiv).on('click','td[cls="m"]',function(){
                    $('#'+options.mHiddenId).val($(this).attr('val'));
                    setButton();
                    hideResult();
                });
                $('#'+options.popDiv).find('.bottom').on('click',"span",function(){
                    showDiv('y');
                });
                
            }

            function showDiv(vn){
                var div=$('#'+options.popDiv);
                var table=setTable(vn);
                div.find('table>tbody').html(table);
                setBottom(div,vn);
                var hiddenId='#'+(vn==='y'?options.yHiddenId:options.mHiddenId);
                var curY=$(hiddenId).val();
                if(curY){
                    div.find('table tr').find("td[cls='"+vn+"'][val='"+curY+"']").addClass('selected');
                }
                if(div.css('visibility')==='hidden'){
                    div.css('visibility','visible');
					div.siblings("i").removeClass("backDown").addClass("backUp").siblings("input").addClass("focus_color");
                };
				if( vn === 'm' ){
					div.find(".title").html(options.titleM);
				}else{
					div.find(".title").html(options.title);
				};
				$(document).bind("click", popClick);
            }
            function hideResult(){
                var div=$('#'+options.popDiv);
                div.css('visibility','hidden');
				div.siblings("i").removeClass("backUp").addClass("backDown").siblings("input").removeClass("focus_color");
				optsFn(options.close,"close");
				$(document).unbind("click", popClick);
            }
			function isFn(obj){
				return !!(obj && obj.constructor && obj.call && obj.apply);
			};
			function optsFn(fn,fnName){
				if( isFn(fn) ){
					fn();
				}else{
					if(fn != null){
						alert(fnName + " is not a function");
					};
				};
			};
			function popClick(e){
				if ($(e.target).closest('.'+options.divClass).length || !$(e.target).closest("body").length) {
					return;
				}
				hideResult();
			}
        }
    });

}).call(window,jQuery);